﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows.Input;
using Present.Commands.Async.Actions;
using Present.Commands.Fluent;

namespace Present.Commands.Sample.AsyncDemo
{
    public class ItemsCollection : ObservableCollection<ItemViewModel>
    {
        public ItemsCollection(IEnumerable<ItemViewModel> collection) : base(collection)
        {
            DeleteCommand = Command.Async
                (
                    AsyncAction.FromDelegates<ItemViewModel, ItemViewModel>()
                        .Do(e =>
                                {
                                    e.Parameter.IsDeleting = true;
                                    Thread.Sleep(2000);
                                    return e.Parameter;
                                })
                        .ToAction()
                        .SuccessVisualState(ItemVisualStates.Delete, item => Remove(item))
                ).ToCommand();

            AddCommand = Command.Async
               (
                   AsyncAction.FromDelegates<object, ItemViewModel>()
                            .Do(x => new ItemViewModel("New Item "))
                            .OnSuccess(Add)
                   .ToAction()
                   .SuccessVisualState(ItemVisualStates.Add)
               ).ToCommand();
        }


        public ICommand DeleteCommand { get; private set; }
        public ICommand AddCommand { get; private set; }
    }

    public static class ItemVisualStates
    {
        public const string Add = "Add";
        public const string Delete = "Delete";
    }
}